/**
 * @module 数据库连接配置
 */

import path from 'path'
import { MysqlConnectionOptions } from 'typeorm/driver/mysql/MysqlConnectionOptions'
import { MongoConnectionOptions } from 'typeorm/driver/mongodb/MongoConnectionOptions'
import { IS_DEV, ROOT_PATH, SRC_PATH } from '@/constants'

// MYSQL数据库连接配置
export const mysqlConfig: MysqlConnectionOptions = {
  type: 'mysql',
  host: process.env.MYSQL_HOST || 'localhost',
  port: +(process.env.MYSQL_PORT || 3306),
  username: process.env.MYSQL_USER_NAME || 'root',
  password: process.env.MYSQL_PASSWORD || 'root',
  database: process.env.MYSQL_DATABASE || 'test',
  charset: process.env.MYSQL_CHARSET || 'UTF8_GENERAL_CI',
  // debug: IS_DEV, 开启后会在终端输出sql语句和sql的trace信息
  // synchronize: IS_DEV, // 自动同步数据库结构，慎用，开启后会更具entities中的实体类自动创建/更新表结构
  logging: IS_DEV ? 'all' : ['error', 'query', 'warn'],
  logger: 'file', // 日志输出到文件
  entities: [path.join(SRC_PATH, 'models/mysql/**/*{.ts,.js}')],
  migrations: [path.join(ROOT_PATH, 'database/migration/mysql/**/*{.ts,.js}')],
  subscribers: [path.join(SRC_PATH, 'models/mysql/subscriber/**/*{.ts,.js}')],
  migrationsTableName: process.env.MYSQL_MIGRATION_TABLE || 'migrations'
}

// MONGODB数据库连接配置
export const mongodbConfig: MongoConnectionOptions = {
  type: 'mongodb',
  host: process.env.MONGODB_HOST || 'localhost',
  port: +(process.env.MONGODB_PORT || 27017),
  username: process.env.MONGODB_USER_NAME || 'root',
  password: process.env.MONGODB_PASSWORD,
  database: process.env.MONGODB_DATABASE || 'test',
  authSource: process.env.MONGODB_AUTH_DATABASE || 'admin',
  useUnifiedTopology: !IS_DEV,
  useNewUrlParser: true,
  // synchronize: IS_DEV, // 自动同步数据库结构，慎用，开启后会更具entities中的实体类自动创建/更新表结构 对mongodb无效
  entities: [path.join(SRC_PATH, 'models/mongo/**/*{.ts,.js}')],
  migrations: [path.join(ROOT_PATH, 'database/migration/mongo/**/*{.ts,.js}')],
  subscribers: [path.join(SRC_PATH, 'models/mongo/subscriber/**/*{.ts,.js}')],
  migrationsTableName: process.env.MONGODB_MIGRATION_TABLE || 'migrations',
  monitorCommands: IS_DEV // 开启后会在终端输出mongodb的操作命令，typeOrm对mongodb的日志支持不是很好，如果要查看mongodb的操作命令，通过配置此项开启
}
